package com.qk.management.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.dto.course.CourseDTO;
import com.qk.entity.Course;
import com.qk.management.mapper.CourseMapper;
import com.qk.management.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: chouh
 * @DateTime: 2025/10/11 19:39
 * @Description:
 */
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;

    @Override
    public List<Course> queryCoursesBySubject(String subject) {
        return courseMapper.queryCoursesBySubject(subject);
    }

    @Override
    public List<Course> queryAllCourses() {
        return courseMapper.selectAllCourses();
    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateCourse(course);
    }

    @Override
    public Course queryCourseById(Integer id) {
        return courseMapper.selectById(id);
    }

    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insertCourse(course);
    }

    @Override
    public void deleteCourse(Integer id) {
        courseMapper.deleteCourse(id);
    }

    @Override
    public PageResult<Course> listCourse(CourseDTO courseDTO) {
        PageHelper.startPage(courseDTO.getPage(),courseDTO.getPageSize());
        List<Course> courses = courseMapper.selectByPage(courseDTO);
        Page<Course> pageInfo = (Page<Course>) courses;
        return PageResult.<Course>builder().total(Long.valueOf(pageInfo.getTotal()).intValue()).rows(pageInfo.getResult()).build();
    }


}
